package com.youlu.campus.admin.course.service.impl;

import com.youlu.campus.admin.course.convert.CourseConvert;
import com.youlu.campus.admin.course.dto.ActivityCourseBindDTO;
import com.youlu.campus.admin.course.dto.ActivityCourseSaveDTO;
import com.youlu.campus.admin.course.dto.ActivityCourseSearchDTO;
import com.youlu.campus.admin.course.service.ActivityCourseService;
import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.ActivityCostStatistics;
import com.youlu.campus.entity.course.ActivityCourse;
import com.youlu.campus.entity.course.ActivityCourseRelation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * @author : [dengbo]
 * @className : ActivityCourseServiceImpl
 * @description : [活动课程]
 * @createTime : [2022/5/16 15:35]
 */
@Service
@Slf4j
public class ActivityCourseServiceImpl implements ActivityCourseService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private CourseConvert courseConvert;

    @Override
    public boolean save(ActivityCourseSaveDTO saveDTO) {
        ActivityCourse course = null;
        if (StringUtils.isNotBlank(saveDTO.getId())) {
            //修改
            course = mongoTemplate.findOne(Query.query(Criteria.where("id").is(saveDTO.getId())), ActivityCourse.class);
            if (course == null) {
                throw new BusinessException("课程信息不存在");
            }
        }
        course = courseConvert.activityCourseSaveDTOToEntity(saveDTO, course);
        mongoTemplate.save(course);
        return true;
    }

    @Override
    public Page<ActivityCourse> list(ActivityCourseSearchDTO dto) {
        Query query = new Query();
        if (StringUtils.isNotBlank(dto.getCourseId())) {
            query.addCriteria(Criteria.where("id").is(dto.getCourseId()));
        }
        if (StringUtils.isNotBlank(dto.getCourseName())) {
            Pattern pattern = Pattern.compile("^.*" + dto.getCourseName() + ".*$", Pattern.CASE_INSENSITIVE);
            query.addCriteria(Criteria.where("name").regex(pattern));
        }
        if (dto.getQiMoBangRole()) {
            query.addCriteria(Criteria.where("creatorId").is(dto.getUserId()));
        }
        query.addCriteria(Criteria.where("deleted").ne(true));
        long count = mongoTemplate.count(query, ActivityCourse.class);
        query.with(Sort.by(Sort.Order.desc("createdTime")));
        PageRequest pageRequest = PageRequest.of(dto.getPage() - 1, dto.getPageSize());
        List<ActivityCourse> datas = mongoTemplate.find(query.with(pageRequest), ActivityCourse.class);
        return PageableExecutionUtils.getPage(datas, pageRequest, () -> count);
    }

    @Override
    public boolean bind(ActivityCourseBindDTO bindDTO) {
        Query query = new Query(Criteria.where("activityId").is(bindDTO.getActivityId()));
        ActivityCourseRelation relation = mongoTemplate.findOne(query, ActivityCourseRelation.class);
        if (relation != null) {
            relation.setCourseId(bindDTO.getCourseId());
            mongoTemplate.save(relation);
            return true;
        }
        relation = new ActivityCourseRelation();
        relation.setActivityId(bindDTO.getActivityId());
        relation.setCourseId(bindDTO.getCourseId());
        relation.setBind(1);
        mongoTemplate.insert(relation);
        return true;
    }
}
